torchd.ui.tooltip = function(element, params) {
	this.init(element, params);
};


torchd.ui.tooltip.prototype.init = function(element, setParams) {
	var self = this;
	var params = {};
	this.element = element;
	jQuery.extend(params,setParams);
	this.animate = params.animate;
	this.animateSpeed = params.animateSpeed;
	this.position = params.placement;
	this.tooltip = torchd.ui.createElement('div').hide().addClass('torchd-ui-tooltip').html(params.html).insertAfter(element);
	
	
	
	
	$(document).mouseover(function() {
		self.hide();
	});
	this.tooltip.data('tooltip', this).mouseover(function(e) {
		e.stopPropagation();
	});
	element.data('tooltip', this).mouseover(function(e) {
		e.stopPropagation();
		$(this).data('tooltip').show();
	});
};

torchd.ui.tooltip.prototype.place = function() {
	var element = this.element;
	// Get element position
	var elementPos = element.offset();
	var elementHeight = element.realHeight();
	var elementWidth = element.realWidth();
	var tooltipHeight = this.tooltip.realHeight();
	var tooltipWidth = this.tooltip.realWidth();
	
	if(!this.position) {
		var vert = '';
		var horz = '';
		// Get element position relative to window 
		var scrollLeft = $(window).scrollLeft();
		var scrollTop = $(window).scrollTop();
		var elementCenter = {
			left:elementPos.left + (elementWidth/2) - scrollLeft,
			top:elementPos.top + (elementHeight/2) - scrollTop
		};
		var windowHeight = $(window).height();
		var windowWidth = $(window).width();
		if(elementCenter.left / windowWidth < .15) {
			horz = 'e';
		}
		else if(elementCenter.left / windowWidth < .85) {
			horz = '';
		}
		else {
			horz = 'w';
		}
		
		if(elementCenter.top / windowHeight < .5) {
			vert = 's';
		}
		else {
			vert = 'n';
		}
		
		position = vert + horz;
	}
	else {
		position = this.position;
	}
	
	this.tooltip.removeClass('torchd-ui-tooltip-placement-n').removeClass('torchd-ui-tooltip-placement-nw').removeClass('torchd-ui-tooltip-placement-ne').removeClass('torchd-ui-tooltip-placement-w').removeClass('torchd-ui-tooltip-placement-e').removeClass('torchd-ui-tooltip-placement-s').removeClass('torchd-ui-tooltip-placement-sw').removeClass('torchd-ui-tooltip-placement-se').addClass('torchd-ui-tooltip-placement-' + position);

	switch(position) {
		case 's':
			this.tooltip.css('top', (elementPos.top + elementHeight).toString() + 'px');
			this.tooltip.css('left', ((elementPos.left + (elementWidth / 2)) - (tooltipWidth/2)).toString() + 'px');
			break;
		case 'se':
			this.tooltip.css('top', (elementPos.top + elementHeight).toString() + 'px');
			this.tooltip.css('left', ((elementPos.left + (elementWidth / 2))).toString() + 'px');
			break;
		case 'sw':
			this.tooltip.css('top', (elementPos.top + elementHeight).toString() + 'px');
			this.tooltip.css('left', ((elementPos.left + (elementWidth / 2)) - tooltipWidth).toString() + 'px');
			break;
		case 'w':
			this.tooltip.css('top', ((elementPos.top + elementHeight/2) - (tooltipHeight/2)).toString() + 'px');
			this.tooltip.css('left', (elementPos.left - tooltipWidth).toString() + 'px');
			break;
		case 'e':
			this.tooltip.css('top', ((elementPos.top + elementHeight/2) - (tooltipHeight/2)).toString() + 'px');
			this.tooltip.css('left', (elementPos.left + elementWidth).toString() + 'px');
			break;
		case 'n':
			this.tooltip.css('top', (elementPos.top - tooltipHeight).toString() + 'px');
			this.tooltip.css('left', ((elementPos.left + (elementWidth / 2)) - (tooltipWidth/2)).toString() + 'px');
			break;
		case 'ne':
			this.tooltip.css('top', (elementPos.top - tooltipHeight).toString() + 'px');
			this.tooltip.css('left', ((elementPos.left + (elementWidth / 2))).toString() + 'px');
			break;
		case 'nw':
			this.tooltip.css('top', (elementPos.top - tooltipHeight).toString() + 'px');
			this.tooltip.css('left', ((elementPos.left + (elementWidth / 2)) - tooltipWidth).toString() + 'px');
			break;
			
			
			
			
	}
};

torchd.ui.tooltip.prototype.show = function() {
	this.place();

	if(this.animate == 'slide') {
		//this.tooltip.show();
		
		var animate = {
			height:tooltipInfo.height,
			width:tooltipInfo.width
		};
		
		var css = {
			height:'0px',
			width:'0px'
		};
		
		
		this.tooltip.css(css).show().animate(animate, this.animateSpeed);
	}
	else if(this.animate == 'fade') {
		this.tooltip.fadeIn(this.animateSpeed);
	}
	else {
		this.tooltip.show();
	}	
};

torchd.ui.tooltip.prototype.hide = function() {

	if(this.animate == 'slide') {
		
		var animate = {
			height:'0px',
			width:'0px'
		};
		switch(this.position) {
			case 'ne':
				animate.top = (parseInt(this.tooltipInfo.top) + parseInt(this.tooltipInfo.realHeight)).toString() + 'px' ;
				break; 
			case 'nw':
				animate.left = this.tooltipInfo.left;
				break;
			case 'se':
				animate.top = top;
				animate.left = this.tooltipInfo.left;
				break;
			case 's':
				animate.top = this.tooltipInfo.top;
				break;
			case 'sw':
				animate.top = this.tooltipInfo.top;
				break;
		}
		
		this.tooltip.animate(animate, this.animateSpeed, function() {
			$(this).hide();
		});
	}
	else if(this.animate == 'fade') {
		this.tooltip.fadeOut(this.animateSpeed);
	}
	else {
		this.tooltip.hide();
	}	
};

